var app = getApp()
var util = require('../../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        taskType:'',
        noDataHide:false,
        listData:[],
        currentPage:0,
        listTotal:0
    },

    /**
     * 生命周期函数--监听页面加载
     * */
    onLoad: function (options) {
        this.setData({
            taskType:options.taskDefineKey
        })
        this.getListData(1)
    },
    /**
     * 生命周期函数--监听上拉刷新
     * */
    onReachBottom: function (){
        if(this.data.listData.length<this.data.listTotal){
            this.getListData(this.data.currentPage+1)
        }else{
            wx.showToast({
                title: '已全部加载',
                icon: 'success',
                duration: 1000
            })
        }
    },

    //获取列表数据
    getListData:function (currentPage) {
        this.setData({
            currentPage:currentPage
        })
        let that = this
        let header;
        header = {
            'Cookie': wx.getStorageSync("sessionid") ,//读取cookie
            'content-type': 'application/x-www-form-urlencoded'
        };
        let params={pageCurrent:this.data.currentPage,taskDefineKey:this.data.taskType};
        wx.showLoading()
        wx.request({
            url: app.globalData.baseUrl + '/safe/emgFlow/getMyTask',
            header: header,
            method: "POST",
            data: params,
            success(res) {
                if(res.data.data.data.length){
                    let tempArr=res.data.data.data;
                    tempArr.forEach(function (item) {
                        let dateObj=new Date();
                        dateObj.setTime(item.emgPlanPublic.createDate);
                        item.createDateStr=util.formatTime(dateObj);
                    });
                    let resultArr=that.data.listData;
                    that.setData({
                        listData: resultArr.concat(tempArr),
                        listTotal:res.data.data.page.total,
                        noDataHide:true
                    });
                }else{
                    that.setData({
                        listData: [],
                        listTotal:0,
                        noDataHide:false
                    });
                }
                //关闭加载
                wx.hideLoading()
            },
            fail(){
                //关闭加载
                wx.hideLoading()
                wx.showToast({
                    title: '加载数据失败',
                    icon: 'none',
                    duration: 1000
                })
            }
        })
    },
    examineClick:function (e) {
        if(this.data.taskType==='usertask4'){//解除审核
            wx.redirectTo({
                url: '/pages/emergency/emerReleasePage/emerReleasePage?emergencyId=' + e.currentTarget.dataset.emergencyId + '&taskId=' + e.currentTarget.dataset.taskId + '&startPlanId=' + e.currentTarget.dataset.startPlanId + '&taskDefineKey=' + this.data.taskType
            })
        }else if(this.data.taskType==='usertask8'){//总结审核
            wx.redirectTo({
                url: '/pages/emergency/emerSummaryPage/emerSummaryPage?emergencyId=' + e.currentTarget.dataset.emergencyId + '&taskId=' + e.currentTarget.dataset.taskId + '&startPlanId=' + e.currentTarget.dataset.startPlanId + '&taskDefineKey=' + this.data.taskType
            })
        }else{
            wx.redirectTo({
                url: '/pages/emergency/emerViewPage/emerViewPage?emergencyId=' + e.currentTarget.dataset.emergencyId + '&taskId=' + e.currentTarget.dataset.taskId + '&startPlanId=' + e.currentTarget.dataset.startPlanId + '&taskDefineKey=' + this.data.taskType
            })
        }
    },
    onClickLeft:function () {
        wx.redirectTo({
            url: '/pages/emergency/emerExamine/emerExamine'
        })
    }

})